import { Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from '@cool-midway/core';
import { MagicCateEntity } from '../../entity/magicCate';
import { MagicEntity } from '../../entity/magic';

// import { MagicService } from '../../service/magic';
import { Context } from 'egg';
/**
 * 商品
 */
@Provide()
@CoolController({
  // 添加通用CRUD接口
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  // 设置表实体
  entity: MagicCateEntity,
  // service:MagicService,
  // 新增的时候插入当前用户ID
  insertParam: async (ctx: Context) => {
    return {
      userId: ctx.admin.userId,
    };
  },
  pageQueryOp: {
    // 让title字段支持模糊查询
    keyWordLikeFields: ['a.name', 'a.target_id', 'a.source'],
    select: ['a.*', 'b.title as magic_title'],
    // 关联表用户表
    leftJoin: [
      {
        // 管理的表
        entity: MagicEntity,
        // 别名
        alias: 'b',
        // 关联条件
        condition: 'a.targetId = b.id',
      },
    ],
    where: async (ctx: Context) => {
      const arr = [];
      if (ctx.request.body.source) {
        arr.push(['a.source = :source', { source: ctx.request.body.source }]);
      }
      return arr;
    },
  },
})
export class MagicCateController extends BaseController {}
